<template>
  <div class="app-container">

    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入组织名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <el-col :span="20" :xs="24">


        <el-form :model="queryParams" :inline="true" ref="queryForm" label-width="100px">
          <el-form-item label="手机号" prop="account">
            <el-input v-model="queryParams.account" placeholder="请输入手机号" clearable size="small"
                      @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <!--          <el-form-item label="昵称" prop="nickName">-->
          <!--            <el-input v-model="queryParams.nickName" placeholder="请输入昵称" clearable size="small"-->
          <!--                      @keyup.enter.native="handleQuery"-->
          <!--            />-->
          <!--          </el-form-item>-->
          <el-form-item label="网关MAC" prop="gatewayMac">
            <el-input v-model="queryParams.gatewayMac" placeholder="请输入网关MAC"
                      clearable size="small" @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
                       v-hasPermi="['smart:geofencing:query']"
            >搜索
            </el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            <el-button type="warning" icon="el-icon-download" size="mini" v-hasPermi="['smart:geofencing:export']"
                       @click="handleExport"
            >导出
            </el-button>
          </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="fencingInfoList" border>
          <el-table-column label="手机号" align="center" prop="account" width="120"/>
          <el-table-column label="网关MAC" align="center" prop="gatewayMac" :show-overflow-tooltip="true"/>
          <!--          <el-table-column label="昵称" align="center" prop="nickName" :show-overflow-tooltip="true"/>-->
          <el-table-column label="家庭名称" align="center" prop="homeName" :show-overflow-tooltip="true"/>
          <el-table-column label="家庭位置" align="center" prop="detailAddress" :show-overflow-tooltip="true"/>
          <el-table-column label="围栏类型" align="center" prop="fencingType" :show-overflow-tooltip="true"/>
          <el-table-column label="围栏半径(米)" align="center" prop="distance" width="120">
            <template slot-scope="scope">
              {{ scope.row.distance * 1000 }}
            </template>
          </el-table-column>
          <el-table-column label="围栏状态" align="center" width="120">

            <template slot-scope="scope">
              <template v-if="scope.row.enable == '1'">
                <div class="myTagOpen" style="margin-bottom: 2px">启用</div>
              </template>
              <template v-else>
                <div class="myTagClose">禁用</div>
              </template>
            </template>
          </el-table-column>
          <el-table-column label="场景名称" align="center" prop="sceneName" :show-overflow-tooltip="true"/>
          <el-table-column label="网关ID" align="center" prop="gatewayId" v-if="false"/>
          <el-table-column label="组织名称" align="center" prop="deptName" :show-overflow-tooltip="true"/>
          <el-table-column label="修改时间" align="center" prop="latestTime" :show-overflow-tooltip="true" width="180"/>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getFencingInfoList"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { exportGeoFencing, listGeoFencing } from '@/api/system/geofencing'
import { deptTreeSelect } from '@/api/system/dept'

export default {
  name: 'index',
  data() {
    return {
      // 总条数
      total: 0,
      fencingInfoList: [],
      loading: true,
      // 部门名称
      deptName: undefined,
      // 部门树选项
      deptOptions: undefined,
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        account: undefined,
        nickName: undefined,
        gatewayMac: undefined,
        deptId: undefined
      }
    }
  },
  created() {
    this.getTreeselect()
    this.getFencingInfoList()
  },
  methods: {
    getFencingInfoList() {
      this.loading = true

      if (!this.queryParams.deptId) {
        // 取出当前用户的组织ID
        this.queryParams.deptId = this.$store.getters.deptId
      }

      listGeoFencing(this.queryParams).then(response => {
        this.total = response.total
        this.fencingInfoList = response.rows
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getFencingInfoList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },

    /** 查询部门下拉树结构 */
    getTreeselect() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data
      })
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.selectedOrgCode = data.id
      this.queryParams.deptId = data.id
      this.getFencingInfoList()
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportGeoFencing(queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(function() {
      })
    }
  }
}
</script>

<style scoped>
</style>
